<?php
/**
 * Created by PhpStorm.
 * Author: 芸签cms www.yunqiancms.com
 * User: sun
 * Date: 2022/6/30
 * Time: 11:51 AM
 */
namespace app\backend\modules\sys\controllers;

use app\backend\controllers\BaseController;
use app\backend\modules\sys\services\ThemeService;
use app\common\models\Theme;
use app\common\models\ThemeFile;
use Illuminate\Support\Facades\DB;

class ThemeController extends BaseController
{
    /**
     * 已安装模版列表页面
     */
    public function installedView(){
        return view("backend.sys.theme.installed");
    }

    /**
     * 已安装模版列表数据
     */
    public function getInstalledList(){
        $list = (new ThemeService())->getInstalledList();
        return $this->successJson('',$list);
    }
    /**
     * 未安装模版列表页面
     */
    public function unInstalledView(){
        return view("backend.sys.theme.uninstalled");
    }

    /**
     * 未安装模版列表数据
     */
    public function getUnInstalledList(){
        $list = (new ThemeService())->getUnInstalledList();
        return $this->successJson('',$list);
    }

    /**
     * 安装模版
     */
    public function doInstall(){
        $theme = trim(request()->theme);
        $model = Theme::site()->select('id','theme')
            ->where('theme',$theme)
            ->first();
        if (!empty($model)) {
            $this->errorJson('模板已经安装');
        }
        (new ThemeService())->installTheme($theme);
        return $this->successJson('');
    }

    /**
     * 卸载模版
     */
    public function doUnInstall(){
        $id = intval(request()->id);
        /*
         * todo 默认模版不能卸载
         * if ($theme == "simpleboot3" || config('cmf_default_theme') == $theme) {
            $this->error("官方自带模板或当前使用中的模板不可以卸载");
        }*/
        DB::transaction(function () use ($id) {
            Theme::where('id',$id)->delete();
            ThemeFile::where('theme_id',$id)->delete();
        });
        return $this->successJson('');
    }

    public function changeStatus(){
        $id = request()->id;
        $type = request()->type;
        $model = Theme::site()->select()
            ->where('id',$id)
            ->first();
        if (empty($model)) {
            $this->errorJson('未找到数据');
        }
        if($type==Theme::TYPE_PC){

            $status=Theme::STATUS_NORMAL;
            if($model->pc_status==Theme::STATUS_NORMAL){
                $status= Theme::STATUS_BAN;
            }else{
                //开启时，其他数据状态改为禁用
                Theme::where('id','!=',$id)->update(['pc_status' =>  Theme::STATUS_BAN]);
            }
            $model->pc_status = $status;
            $model->save();
        } elseif ($type==Theme::TYPE_H5){
            $status=Theme::STATUS_NORMAL;
            if($model->h5_status==Theme::STATUS_NORMAL){
                $status= Theme::STATUS_BAN;
            }else{
                //开启时，其他数据状态改为禁用
                Theme::where('id','!=',$id)->update(['h5_status' =>  Theme::STATUS_BAN]);
            }
            $model->h5_status = $status;
            $model->save();
        } else {
            $this->errorJson('终端类型错误');
        }
        return $this->successJson();
    }

    /**
     * 更新模版
     */
    public function update(){

    }

    /**
     * 模版文件列表页面
     */
    public function themeFile(){

    }
    /**
     * 模版文件列表数据
     */
    public function getThemeFileList(){

    }

    public function design(){

    }
    public function getDesignData(){

    }
    public function saveDesign(){

    }
}